package com.ias.agdis_r.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.task.FLG10TaskBean;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.util.S;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by guoxr on 2015/12/2.
 */
public class QJTaskSearchAdapter extends BaseAdapter {
    private List<FLG10TaskBean> data;
    private LayoutInflater mInflater;
    private Context context;
    private FLG10TaskBean FLG10Task;

    public QJTaskSearchAdapter(Context context, List<FLG10TaskBean> data) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
        this.context = context;
        sort();
    }

    static class ViewHolder {
        TextView txtHangbanhao, txtJihao, txtJiwei, txtJiXing,
                txtZhiXing, txtPnm, txtShijian;
        ImageView ivadid;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_qjtasksearch_adapter, null);
            holder = new ViewHolder();
            holder.ivadid = (ImageView) convertView.findViewById(R.id.item_qjmonitor_adidImage);
            holder.txtHangbanhao = (TextView) convertView.findViewById(R.id.item_qjmonitor_fln);
            holder.txtJiwei = (TextView) convertView.findViewById(R.id.item_qjmonitor_psn);
            holder.txtShijian = (TextView) convertView.findViewById(R.id.item_qjmonitor_time);
            holder.txtZhiXing = (TextView) convertView.findViewById(R.id.item_qjmonitor_tem);
            holder.txtJiXing = (TextView) convertView.findViewById(R.id.item_qjmonitor_acn);
            holder.txtPnm = (TextView) convertView.findViewById(R.id.item_qjmonitor_state);
            holder.txtJihao = (TextView) convertView.findViewById(R.id.item_qjmonitor_regnumber);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        FLG10Task = data.get(position);
        if (null != FLG10Task) {
            holder.txtHangbanhao.setText(FLG10Task.getFln());
            if ("A".equals(FLG10Task.getAod())) {
                holder.ivadid.setImageResource(R.drawable.adida_red);
            } else {
                holder.ivadid.setImageResource(R.drawable.adidd);
            }
            holder.txtZhiXing.setText(FLG10Task.getTem());
            String pnm = FLG10Task.getTst();
            if (pnm.equals(context.getString(R.string.paifa))) {
                holder.txtPnm.setText(context.getString(R.string.unaccept));
                holder.txtPnm.setBackgroundResource(R.drawable.textview_background2);
            } else if (MessageModel.TCXX.equals(FLG10Task.getPid())
                    || MessageModel.TRJT.equals(FLG10Task.getPid())
                    || MessageModel.TEND.equals(FLG10Task.getPid())
                    || MessageModel.T_QJHS_YQ.equals(FLG10Task.getPid())
                    || MessageModel.TTRN.equals(FLG10Task.getPid())) {
                holder.txtPnm.setText(pnm);
                holder.txtPnm.setBackgroundResource(R.drawable.textview_background3);
            } else {
                holder.txtPnm.setText(pnm);
                holder.txtPnm.setBackgroundResource(R.drawable.textview_background);
            }
            holder.txtShijian.setText(context.getString(R.string.time)+S.FormatTime2(FLG10Task.getTime()));
            holder.txtJiwei.setText(FLG10Task.getPsn() != null ? context.getString(R.string.VFS_2) + FLG10Task.getPsn() : context.getString(R.string.VFS_2));
            holder.txtJiXing.setText(FLG10Task.getAcn() != null ? context.getString(R.string.VFS_1) + FLG10Task.getAcn() : context.getString(R.string.VFS_1));
            holder.txtJihao.setText(FLG10Task.getReg() != null ? context.getString(R.string.VFS_3) + FLG10Task.getReg() : context.getString(R.string.VFS_3));
        }
        return convertView;
    }

    @Override
    public int getCount() {
        if (null != data) {
            return data.size();
        } else {
            return 0;
        }
    }

    @Override
    public FLG10TaskBean getItem(int position) {
        if (null != data) {
            return data.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void notifyDataSetChanged(){
        sort();
        super.notifyDataSetChanged();
    }

    private void sort()
    {
        //通过比较器实现比较排序
        Collections.sort(data, new Comparator<FLG10TaskBean>() {
            public int compare(FLG10TaskBean task1, FLG10TaskBean task2) {
                String pidx1 = task1.getPidx();
                String pidx2 = task2.getPidx();
                if (null == pidx1){
                    pidx1 = "";
                }
                if (null == pidx2){
                    pidx2 = "";
                }
                int pidxCom = pidx1.compareTo(pidx2);
                if(pidxCom==0) {
                    String time1 = task1.getTime();
                    String time2 = task2.getTime();
                    if (null == time1){
                        time1 = "";
                    }
                    if (null == time2){
                        time2 = "";
                    }
                    int timeCom = time2.compareTo(time1);
                    return timeCom;
                }

                return pidxCom;
            }
        });
    }
}
